import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import ru.ksu.ktk.diploma.sdi.mapping.loaders.ThesauriLoader;
import ru.ksu.ktk.diploma.sdi.util.OntClassUtil;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;

@ContextConfiguration(locations = {"classpath:META-INF/spring/mapping-beans.xml", "classpath:test-dao-beans.xml",
    "classpath:test-properties-beans.xml", "classpath:META-INF/spring/analyzer-beans.xml"})
public class FilterFacetsTestCase extends AbstractTestNGSpringContextTests {

    private static final String FOLDER_NAME = "C://temp//emku//desc";
    private static final String VECTOR_FILE_PATH = String.format("%s//%s", FOLDER_NAME, "emku-name-tn.txt");
    private static final String OUTPUT_FILE_PATH = String.format("%s//%s", FOLDER_NAME, "emku-name-tn-filtered.txt");
    private static final String[] FILTER_FACET_NAME = {"aatnum_300264092"};

    @Autowired
    ThesauriLoader thesauriLoader;

    OntModel thesauri;

    @Test
    public void testFilterFacets() throws FileNotFoundException, IOException {
        thesauri = thesauriLoader.load();
        List<String> descList = FileUtil.readValues(VECTOR_FILE_PATH);
        List<String> filteredList = new ArrayList<String>();
        for (String descName : descList){
            OntClass descBlock = getDescriptorBlock(descName);
            if (isSatisfied(descBlock)){
                filteredList.add(descName);
            }
        }
        FileUtil.write(filteredList, OUTPUT_FILE_PATH);
    }

    private boolean isSatisfied(OntClass descBlock) {
        List<OntClass> superClasses = OntClassUtil.getAllSuperClasses(descBlock);
        List<OntClass> classList = new ArrayList<OntClass>();
        classList.add(descBlock);
        classList.addAll(superClasses);
        for (OntClass ontClass : classList){
            for (String str : FILTER_FACET_NAME){
                if (ontClass.getURI().contains(str)) return true;
            }
        }
        return false;
    }

    private OntClass getDescriptorBlock(String descriptorName) {
        OntClass descriptorBlock = thesauri.getOntClass(descriptorName);
        return descriptorBlock;
    }
}
